<?php

	function get_username_from_url_twitter($url)
	{
		$url = trim($url, " /");
		return substr($url, strrpos($url, "/")+1);
	}

	function get_friends_twitter($url)
	{
		$username = get_username_from_url('twitter', $url);
		if ($username)
		{
			$result = file_get_contents("http://twitter.com/statuses/friends/$username.json");
			
			if ($result)
			{
				$js_friends = json_decode($result);
				$return = array();
				
				foreach ($js_friends as $friend)
				{
					$tmp=new stdClass;
					$tmp->url = "http://twitter.com/{$friend->screen_name}";
					$tmp->username = $friend->screen_name;
					$tmp->name = $friend->name;
					
					$return[] = $tmp;
				}
				
				return $return;
			}
		}
		
		return false;
	}
	
	function get_tags_twitter($url)
	{
	
	}
	
	function get_location_twitter($url)
	{
		global $USER_DETAILS;
		
		if (!$USER_DETAILS) $USER_DETAILS = array();
		
		if (isset($USER_DETAILS[$url]->location)) return $USER_DETAILS[$url]->location;
		
		$username = get_username_from_url('twitter', $url);
		if ($username)
		{
			$result = file_get_contents("http://twitter.com/users/show/$username.json");
			
			if ($result)
			{
				$js = json_decode($result);
				
				$USER_DETAILS[$url] = $js;
				
				if (isset($js->location)) 
					return $js->location;
			}
		}
		
		return false;
	}
	
	function get_user_icon_twitter($url)
	{	
		global $USER_DETAILS;
		
		if (!$USER_DETAILS) $USER_DETAILS = array();
		
		if (isset($USER_DETAILS[$url]->profile_image_url)) return $USER_DETAILS[$url]->profile_image_url;
		
		$username = get_username_from_url('twitter', $url);
		if ($username)
		{
			$result = file_get_contents("http://twitter.com/users/show/$username.json");
			
			if ($result)
			{
				$js = json_decode($result);
				
				$USER_DETAILS[$url] = $js;
				
				if (isset($js->profile_image_url)) 
					return $js->profile_image_url;
			}
		}
		
		return false;
		
	}
	
?>